package com.app.lxx.pregnancyberth.service;

import android.util.Log;
import android.widget.Toast;

import com.app.lxx.pregnancyberth.BuildConfig;
import com.app.lxx.pregnancyberth.MyApplication;
import com.blankj.utilcode.util.ToastUtils;
import com.lztek.toolkit.Lztek;

public class ButtonService {
    private static final int GPIO_PORT = 228;//新设备的k6 对应开发文档gpio号 228
    private static boolean isRuning = false;
    private static Lztek mLztek;

    public static void startWork() {
        Log.i("sButtonServiceS", "startWork开始执行");
        isRuning = true;

        if (BuildConfig.DEBUG) {
            Toast.makeText(MyApplication.context, "调试模式 忽略初始化", Toast.LENGTH_LONG).show();
            return;
        }
        try {
            // gpio Lztek 影响应用，闪退 zjq
//            mLztek = Lztek.create(MyApplication.context);
//            if (!mLztek.gpioEnable(GPIO_PORT)) {
//                Toast.makeText(MyApplication.context, "GPIO[" + GPIO_PORT + "] enable failed", Toast.LENGTH_LONG).show();
//                return;
//            }
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MyApplication.context, "GPIO[" + GPIO_PORT + "] enable failed1"+"\n"+e.getMessage(), Toast.LENGTH_LONG).show();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                        doWork();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private static void doWork() {
        long time = 0;
        while (isRuning) {
            String status = "";
            try {
//                status = GPIO.getGpioString("/sys/class/gpio_sw/PD4/data");
                status = mLztek.getGpioValue(GPIO_PORT) + "";

            } catch (Exception e) {
                // e.printStackTrace();
                Log.d("sss", e.getMessage());
            }
            if ("0".equals(status)) {
                Log.e("GPIO_PORT=", "\"0\".equals(status)=" + "0".equals(status));

                long newtime = System.currentTimeMillis();
                if (newtime - time > 800) {
                    time = newtime;
                    //呼叫/挂断
                    //发送广播
                    // Intent intent1 = new Intent();
                    // intent1.setAction("com.app.lxx.pregnancyberth.service.ButtonService");
                    // sendBroadcast(intent1);
                    try {

                        DataBus.post("callButton", new DataBus.Message(1));
                        Log.e("GPIO_PORT=", "post=" + status);

//                        new LocalBroadcastUtils(MyApplication.context).startLocalBroadcast(VoipP2PActivity.CallPhoneIntent);
                    } catch (Exception e) {
                        e.printStackTrace();
                        ToastUtils.showShort(e.getMessage());
                    }
                }
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void stopWork() {
        isRuning = false;
    }
}
